package com.furtherstar.auth.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.furtherstar.auth.controller.dto.PositionQuery;
import com.furtherstar.auth.controller.dto.PositionUserQuery;
import com.furtherstar.auth.dao.enums.Disabled;
import com.furtherstar.auth.dao.mapper.CorpPositionMapper;
import com.furtherstar.auth.dao.mapper.CorpWorkMapper;
import com.furtherstar.auth.dao.mapper.UserRoleMapper;
import com.furtherstar.auth.dao.model.CorpPosition;
import com.furtherstar.auth.dao.model.CorpPositionExample;
import com.furtherstar.auth.dao.model.CorpPositionExample.Criteria;
import com.furtherstar.auth.dao.model.CorpWorkExample;
import com.furtherstar.auth.dao.model.UserRoleExample;
import com.furtherstar.auth.service.CorpPositionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageSerializable;
import com.yummy.convention.exception.AssertUtil;
import com.yummy.convention.lang.IDGenerator;
import com.yummy.security.User;
import com.yummy.web.context.RequestContext;
import com.yummy.web.security.bean.SimpleUser;

@Service
public class CorpPositionServiceImpl implements CorpPositionService{
	
	@Autowired
	private CorpPositionMapper corpPositionMapper;
	@Autowired
	private CorpWorkMapper corpWorkMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;

	@Override
	public Long add(CorpPosition position) {
		AssertUtil.isTrue(!exist(position),"职位不可重复");
		createMark(position);
		corpPositionMapper.insert(position);
		return position.getId();
	}

	private void createMark(CorpPosition position) {
		position.setId(IDGenerator.getId());
		position.setDisabled(Disabled.normal.ordinal());
		User user = RequestContext.getCurrentUser();
		position.setLmNickname(user.getNickname());
		position.setLmUserid((Long) user.getUserid());
		position.setLmTime(new Date());
	}

	private boolean exist(CorpPosition position) {
		CorpPositionExample example = new CorpPositionExample();
		Criteria criteria = example.createCriteria().andCorpIdEqualTo(position.getCorpId())
			.andNameEqualTo(position.getName());
		if(position.getId() != null) {
			criteria.andIdNotEqualTo(position.getId());
		}
		return corpPositionMapper.countByExample(example) != 0;
	}

	@Override
	public Long update(CorpPosition position) {
		AssertUtil.isTrue(!exist(position),"职位不可重复");
		optMark(position);
		corpPositionMapper.updateByPrimaryKeySelective(position);
		return position.getId();
	}

	private void optMark(CorpPosition position) {
		User user = RequestContext.getCurrentUser();
		position.setLmNickname(user.getNickname());
		position.setLmUserid((Long) user.getUserid());
		position.setLmTime(new Date());
	}

	@Override
	public Long enable(Long positionId) {
		CorpPosition position = new CorpPosition();
		position.setId(positionId);
		position.setDisabled(Disabled.normal.ordinal());
		optMark(position);
		int row = corpPositionMapper.updateByPrimaryKeySelective(position);
		AssertUtil.isTrue(row == 1,"找不到记录");
		return positionId;
	}

	@Override
	public Long disable(Long positionId) {
		CorpPosition position = new CorpPosition();
		position.setId(positionId);
		position.setDisabled(Disabled.disabled.ordinal());
		optMark(position);
		int row = corpPositionMapper.updateByPrimaryKeySelective(position);
		AssertUtil.isTrue(row == 1,"找不到记录");
		return positionId;
	}

	@Override
	@Transactional
	public Boolean delete(Long positionId) {
		int row = corpPositionMapper.deleteByPrimaryKey(positionId);
		AssertUtil.isTrue(row == 1,"找不到记录");
		deleteRelated(positionId);
		return true;
	}

	private void deleteRelated(Long positionId) {
		deleteCorpWorkByPosition(positionId);
		deleteUserRoleByPosition(positionId);
	}

	private void deleteUserRoleByPosition(Long positionId) {
		UserRoleExample example = new UserRoleExample();
		example.createCriteria().andPositionIdEqualTo(positionId);
		userRoleMapper.deleteByExample(example);
	}

	private void deleteCorpWorkByPosition(Long positionId) {
		CorpWorkExample example = new CorpWorkExample();
		example.createCriteria().andPositionIdEqualTo(positionId);
		corpWorkMapper.deleteByExample(example);
	}

	@Override
	public PageSerializable<CorpPosition> pageQuery(PositionQuery positionQuery) {
		CorpPositionExample example = new CorpPositionExample();
		Criteria criteria = example.createCriteria();
		if(positionQuery.getCorpId() != null) {
			criteria.andCorpIdEqualTo(positionQuery.getCorpId());
		}
		if(positionQuery.getPositionName() != null) {
			criteria.andNameLike("%"+positionQuery.getPositionName()+"%");
		}
		PageHelper.startPage(positionQuery.getPageNum(), positionQuery.getPageSize());
		return PageSerializable.of(corpPositionMapper.selectByExample(example));
	}

	@Override
	public PageSerializable<CorpPosition> pageAvaliable(PositionQuery positionQuery) {
		CorpPositionExample example = new CorpPositionExample();
		Criteria criteria = example.createCriteria().andDisabledEqualTo(Disabled.normal.ordinal());
		if(positionQuery.getCorpId() != null) {
			criteria.andCorpIdEqualTo(positionQuery.getCorpId());
		}
		if(positionQuery.getPositionName() != null) {
			criteria.andNameLike("%"+positionQuery.getPositionName()+"%");
		}
		PageHelper.startPage(positionQuery.getPageNum(), positionQuery.getPageSize());
		return PageSerializable.of(corpPositionMapper.selectByExample(example));
	}

	@Override
	public PageSerializable<SimpleUser> pageUsers(PositionUserQuery positionUserQuery) {
		PageHelper.startPage(positionUserQuery.getPageNum(), positionUserQuery.getPageSize());
		return PageSerializable.of(corpWorkMapper.queryDistinctUsers(positionUserQuery));
	}

}
